package com.message.notify;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Settings;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.message.notify.utils.SharedPreferencesManager;

public class LoginAcitvity  extends AppCompatActivity {
    SharedPreferencesManager instance;
    SharedPreferences.Editor edit;
    CheckBox rem_password ;
    CheckBox rem_auto_login ;
    EditText account_e ;
    EditText password_e;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getSupportActionBar().setTitle("GNSS告警信息管理系统");
        instance = SharedPreferencesManager.getInstance(LoginAcitvity.this);
        edit = instance.getSharedPreferences().edit();
        //init
        account_e = this.findViewById(R.id.et_account);
        password_e = this.findViewById(R.id.et_password);
        rem_password = this.findViewById(R.id.rem_password);//记住密码
        rem_auto_login = this.findViewById(R.id.auto_login);//自动登录

        //给CheckBox设置事件监听
//        rem_password.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView,
//                                         boolean isChecked) {
//                // TODO Auto-generated method stub
//                if(isChecked){
//
//                }else{
//
//                }
//            }
//        });


        Boolean remember_pass = instance.getSharedPreferences().getBoolean("remember_pass", false);
        Boolean auto_login = instance.getSharedPreferences().getBoolean("auto_login", false);
        Boolean isFirstRun = instance.getSharedPreferences().getBoolean("is_first_run", true);
        if (isFirstRun) {
            // 显示对话框提示用户开启通知权限
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("通知权限");
            builder.setMessage("为了让您及时收到重要通知，请开启应用的通知权限。");
            builder.setPositiveButton("开启", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // 引导用户前往系统设置页面开启通知权限
                    Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                    intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
                    startActivity(intent);
                }
            });
            builder.setNegativeButton("取消", null);
            builder.show();

            // 记录应用已经运行过一次，以便下次不再显示该提示
            edit.putBoolean("is_first_run",false);
            edit.apply();

        }
        if(remember_pass){
            rem_password.setChecked(true);
            account_e.setText(instance.getSharedPreferences().getString("username",null));
            password_e.setText(instance.getSharedPreferences().getString("password",null));
            if(auto_login){//在记住密码的前提下，如果勾选自动登录，则打开app时即自动登录
                rem_auto_login.setChecked(true);
                login(null);
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            Thread.sleep(1000); // 休眠1秒
//                            /**
//                             * 延时执行的代码
//                             */
//                            login(null);
//                        } catch (InterruptedException e) {
//                            throw new RuntimeException(e);
//                        }
//                    }
//                }).start();

            }
        }else{
            rem_password.setChecked(false);
//            account_e.setText("");
//            password_e.setText("");
        }
    }

    public void login(View view){

        String account = account_e.getText().toString();

        String password = password_e.getText().toString();
        if("".equals(account)){
            Toast.makeText(this, "账号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }else if("".equals(password)){
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }


        boolean remember_pass = rem_password.isChecked();//是否记住密码
        boolean auto_login = rem_auto_login.isChecked();//是否自动登录




        if(remember_pass){//记住密码
            edit.putBoolean("remember_pass",true);
            edit.putString("username",account);
            edit.putString("password",password);
            edit.apply();
            if(auto_login){//自动登录
                edit.putBoolean("auto_login",true);
                edit.apply();
            }else{//不记住密码
                edit.putBoolean("auto_login",false);
                edit.apply();
            }
        }else{//不记住密码
            edit.putBoolean("remember_pass",false);
//            edit.putString("username","");
//            edit.putString("password","");
            edit.apply();
        }




        Intent intent = new Intent();
        intent.putExtra("username",account);
        intent.putExtra("password",password);
        intent.setClass(this, MainActivity.class);
        startActivity(intent);
    }
}
